﻿using System;
using System.Windows.Forms;
using Microsoft.VisualBasic.Logging;
using SmartMotion.AllPages;
using SmartMotion.AllPages.Debug;
using SmartMotion.AllPages.SysConfig;
using SmartMotion.Core;
using SmartMotion.Core.Mgrs;
using SmartMotion.Pages;
using SmartMotion.Pages.DockPages;
using SmartMotion.Properties;
using Sunny.UI;
using Log = SmartMotion.Core.Helpers.Log;

namespace SmartMotion
{

    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {

            try
            {
                #region 异常处理
                Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
                Application.ThreadException += Application_ThreadException;
                AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
                #endregion

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                RegisterType();
                FlowsMgr.Ins.LoadFlows();
                Log.Ins.Info("Start!");
                Application.Run(new FrmMain());
                Log.Ins.Info("Stop!");
            }
            catch (Exception e)
            {
               Log.Ins.Fatal($"发生致命异常{e.Message}\r{e.StackTrace}");
               UIMessageBox.ShowError($"发生致命异常{e.Message}\r{e.StackTrace}");
            }
        }

        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            if (e.ExceptionObject is Exception ex)
            {
                Log.Ins.Fatal($"发生致命异常{ex.Message}\r{ex.StackTrace}");
                UIMessageBox.ShowError($"发生致命异常{ex.Message}\r{ex.StackTrace}");
            }
        }

        private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            Exception ex = e.Exception;
            Log.Ins.Fatal($"发生致命异常{ex.Message}\r{ex.StackTrace}");
            UIMessageBox.ShowError($"发生致命异常{ex.Message}\r{ex.StackTrace}");
        }

        private static void RegisterType()
        {
            var container = UnityIoc.Ins;
            //< register type = "Sunny.UI.UIPage,SunnyUI" mapTo = "Project2.FrmAuto, Project2" name = "FrmAuto" />
            //< 注册     类型 = "基类命名空间.基类,基类dll" 映射="实现类命名空间.实现类,实现类dll" 索引名称="索引名称"/>
            //< register type = "Sunny.UI.UIPage,SunnyUI" mapTo = "Project2.FrmManual, Project2" name = "FrmManual" />
            //container.RegisterType<UIPage, FrmProductMgr>("FrmProductMgr");
            container.RegisterSingleton<FrmProductMgr>();
            container.RegisterSingleton<FrmFlowEdit>();
            container.RegisterSingleton<FrmStationDebug>();
            container.RegisterSingleton<FrmIODebug>();
            container.RegisterSingleton<FrmMotionConfig>();
            container.RegisterSingleton<FrmIOConfig>();
            container.RegisterSingleton<FrmStationConfig>();
            container.RegisterSingleton<FrmDeviceConfig>();
            container.RegisterSingleton<FrmToolBox>(); //节省代码写单例模式
            container.RegisterSingleton<FrmSolutionMgr>();
            container.RegisterSingleton<FrmProperty>();
            container.RegisterSingleton<FrmDebug>();
            container.RegisterSingleton<FrmParms>();
            container.RegisterSingleton<FrmVar>();
            container.RegisterType<FrmEdit>();
            ProductMgr.Ins.Open(Settings.Default.ProductFilePath);
            IOMgr.Ins.Load();
            MotionMgr.Ins.Load();
            StationsMgr.Ins.Load();
        }
    }
}
